package com.abe.libquick.api;

import android.content.Context;
import android.text.TextUtils;

import com.abe.libcore.base.http.RetrofitUtils;
import com.abe.libcore.base.http.interceptor.MoreBaseUrlEntity;
import com.abe.libcore.utils.NetWorkUtils;
import com.abe.libcore.utils.context.ContextUtils;
import com.abe.libquick.R;
import com.abe.libquick.utils.quick.ToastTool;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;

public class HttpHelper {
    protected static Retrofit appRetrofit(MoreBaseUrlEntity[] moreBaseUrlEntity, String baseUrl) {
        return RetrofitUtils.INSTANCE
                .getJsonRetrofit(
                        ContextUtils.Companion.getContext(),
                        moreBaseUrlEntity,
                        JsonHelper.gson(JsonHelper.JSON_C_SHAPE))
                .baseUrl(baseUrl)
                .build();
    }


    public static void warn() {
        if (!isNetworkAvailable(ContextUtils.Companion.getContext())) {
            ToastTool.show(R.string.network_not_connect);
        }
    }

    /**
     * 判断网络是否可用
     */
    public static Boolean isNetworkAvailable(Context context) {
        return NetWorkUtils.INSTANCE.getNetWorkState(context) != NetWorkUtils.NETWORK_NONE;
    }

    public static RequestBody postBody(String json) {
        return RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), json);
    }

    public static RequestBody postBody(Object o) {
        return RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8")
                , JsonHelper.gson(JsonHelper.JSON_C_SHAPE).toJson(o));
    }

    //图文-普通字段
    public static RequestBody createFormBody(String content) {
        return RequestBody.create(MediaType.parse("multipart/form-data"), content);
    }

    public static Map<String, RequestBody> generateRequestBody(Map<String, String> requestDataMap) {
        Map<String, RequestBody> requestBodyMap = new HashMap<>();
        for (String key : requestDataMap.keySet()) {
            String content = requestDataMap.get(key) == null ? "" : requestDataMap.get(key);
            content = TextUtils.isEmpty(content) ? "" : content;
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), content);
            requestBodyMap.put(key, requestBody);
        }
        return requestBodyMap;
    }

    //图文Multipart, fieldName为表单字段名
    public static MultipartBody.Part createFilePart(String fieldName, String filePath) {
        File file = new File(filePath);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        return MultipartBody.Part.createFormData(fieldName, file.getName(), requestFile);
    }

    public static void addParameter(HashMap<String, RequestBody> map, String key, Object o) {
        RequestBody body = null;
        if (o instanceof String) {
            body = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), (String) o);
        } else if (o instanceof File) {
            body = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), (File) o);
        }
        if (body != null)
            map.put(key, body);
    }

    public static MultipartBody.Part imgPart(String filePath) {
        return imgPart("file", filePath);
    }

    public static MultipartBody.Part imgPart(String key, String filePath) {
        File file = new File(filePath);
        RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpg"), file);
        String fileName = "";
        try {
            fileName = URLEncoder.encode(file.getName(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return MultipartBody.Part.createFormData(key, fileName, requestFile);
    }
}
